package com.report.reportserver.controller;

import java.nio.charset.StandardCharsets;
import java.util.*;

import com.report.reportserver.model.Student;
import com.report.reportserver.service.StudentService;
import com.report.reportserver.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @ ClassName: StudentController
 * @ Description：按照需求返回Student对象
 * @ Modified By：
 * @ Version:
 * @ Author     ：LiYing
 * @ Date       ：Created in 11:26 2022/3/29
 */
@Controller
@RequestMapping(value = "/student")
public class StudentController {

    @Autowired
    StudentService studentService;

    @RequestMapping(value = "/name")
    public @ResponseBody
    List<Student> name(HttpServletRequest request) {
        String name = request.getParameter("name");
        List<Student> list = studentService.queryStudentByName(name);
        return list;
    }

    @RequestMapping(value = "/id")
    public @ResponseBody
    Student id(HttpServletRequest request) {
        int id = Integer.parseInt(request.getParameter("id"));
        Student student = studentService.queryStudentById(id);
        return student;
    }

    @RequestMapping(value = "/number")
    public @ResponseBody
    Student number(HttpServletRequest request) {
        String number = request.getParameter("number");
        Student student = studentService.queryStudentByNumber(number);
        return student;
    }

    @RequestMapping(value = "/all")
    public @ResponseBody
    List<Student> all() {
        List<Student> list = new ArrayList<>();
        List<Integer> ids = studentService.queryStudentAllId();
        for (Integer id : ids) {
            list.add(studentService.queryStudentById(id));
        }
        return list;
    }

    @RequestMapping(value = "/numbers")
    public @ResponseBody
    List<Student> numbers(HttpServletRequest request) {
        int number1 = Integer.parseInt(request.getParameter("number1"));
        int number2 = Integer.parseInt(request.getParameter("number2"));

        List<Student> list = studentService.queryStudentByNumber(number1, number2);
        return list;
    }

    @RequestMapping(value = "/excelinsert")
    public @ResponseBody
    String ExcelParse(@RequestParam("file") MultipartFile file) {
        String name = file.getOriginalFilename();

        if (!name.substring(name.length() - 4).equals(".xls") && !name.substring(name.length() - 5).equals(".xlsx")) {
            return "文件解析错误";
        }
        List<Student> list = new ArrayList<>();
        try {
            list = ExcelUtil.excelToStudent(file.getInputStream());

            for (Student student : list) {
                studentService.insertStudent(student);
            }
        } catch (Exception e) {
            //异常类型
            String eName = e.toString().split(":")[0];

            e.printStackTrace();

            if (eName.split("\\.").length >= 4 && eName.split("\\.")[3].equals("DuplicateKeyException")) {
                return "There is DuplicateKey in Your File";
            }
            return "error";
        }
        return "done";
    }
}
